//
//  YZLCarRedioViewController.m
//  Car
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLCarRedioViewController.h"
#import "HTTPTOOL.h"
#import "YZLCarRedioModel.h"
#import "YZLCarRedioCell.h"
#import "YZLCarRedioPlayerViewController.h"

@interface YZLCarRedioViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *redioArr;
@property (nonatomic, strong) NSMutableArray *album_bigImgurlArr;
@property (nonatomic, strong) NSMutableArray *album_idArr;
@property (nonatomic, strong) NSMutableArray *album_nameArr;
@property (nonatomic, strong) NSMutableArray *album_descritionArr;
@property (nonatomic, strong) UIActivityIndicatorView *activityView;

@end

@implementation YZLCarRedioViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - 数据处理
- (void)dataHandle {
    
    NSString *str = @"http://app.api.autohome.com.cn/autov5.0.0/News/newsmyradiolist-pm1-tp0.json";
    [HTTPTOOL GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        NSArray *arr = [[result objectForKey:@"result"] objectForKey:@"list"];
        self.redioArr = [NSMutableArray array];
        self.album_bigImgurlArr = [NSMutableArray array];
        self.album_idArr = [NSMutableArray array];
        self.album_nameArr = [NSMutableArray array];
        self.album_descritionArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            
            YZLCarRedioModel *model = [YZLCarRedioModel CarRedioWithDic:dic];
            [self.redioArr addObject:model];
            [self.album_idArr addObject:[dic objectForKey:@"album_id"]];
            [self.album_bigImgurlArr addObject:[dic objectForKey:@"album_bigImgurl"]];
            [self.album_nameArr addObject:[dic objectForKey:@"album_name"]];
            [self.album_descritionArr addObject:[dic objectForKey:@"album_descrition"]];
            
        }
        
        [self.tableView reloadData];
        [self.activityView stopAnimating];
        
    } fail:^(NSError *error) {
        
    }];
}

#pragma mark - tableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 80;
    
    UIImageView *view = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    view.image = [UIImage imageNamed:@"背景"];
    self.tableView.backgroundView = view;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"汽车电台";
    
    [self createTableView];
    [self dataHandle];
    
    //bar颜色
    [self navigationbarColor];
    
    //指示器
    self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    self.activityView.color = [UIColor whiteColor];
    self.activityView.center = self.view.center;
    [self.view addSubview:self.activityView];
    //开始动画
    [self.activityView startAnimating];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.redioArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YZLCarRedioModel *model = [self.redioArr objectAtIndex:indexPath.row];
    YZLCarRedioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"redio"];
    if (cell == nil) {
        cell = [[YZLCarRedioCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"redio"];
    }
    
    cell.model = model;
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    return cell;
}

#pragma mark - 点击cell跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YZLCarRedioPlayerViewController *vc = [[YZLCarRedioPlayerViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.album_bigImgurlStr = self.album_bigImgurlArr[indexPath.row];
    vc.album_idStr = [NSString stringWithFormat:@"%@", self.album_idArr[indexPath.row]];
    vc.album_name = self.album_nameArr[indexPath.row];
    vc.album_descritionStr = self.album_descritionArr[indexPath.row];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
